<template>
    <el-skeleton :loading="loading" animated>
      <template #default>
        <div>
          <el-row :gutter="20">
            <!--            左侧商品图部分-->
            <el-col :span="10">
              <sku-pic-list :imgList="skuItemInfo.skuPictureList" v-if="showImg"></sku-pic-list>
            </el-col>
            <!--            右侧SKU信息以及销售属性部分-->
            <el-col :span="14">
              <div>
                <h3>{{ skuItemInfo?.sku?.skuTitle }}</h3>
                <div>
                  <span class="price_class">￥{{ skuItemInfo?.sku?.price }}</span>
                </div>
                <div style="margin-top: 20px">
                  <div v-for="item in skuItemInfo?.skuSaleAttrWithValueWithSkuIdsVOList" :key="item.id">
                    <el-row>
                      <el-col :span="3">{{ item.attrName }}</el-col>
                      <el-col :span="21" >
                        <el-space wrap>
                          <button
                              v-for="(valueItem,i) in item.skuSaleAttrWithValueWithSkuIdsVOList" :key="i"
                              :class="valueItem?.skuIds.indexOf(skuItemInfo.sku?.skuId)!==-1 ? 'skuValue_act_class' : 'skuValue_inc_class'"
                              @click="selectItem(item,valueItem.skuIds)">{{ valueItem.attributeValue }}
                          </button>
                        </el-space>
                      </el-col>
                    </el-row>
                    <hr>
                  </div>
                </div>
                <div style="margin-top: 50px">
                  库存:{{ skuItemInfo?.sku?.stock }}
                </div>
                <div style="margin-top: 50px">
                  <el-input-number v-model="num"/>
                </div>
                <div style="margin-top: 50px">
                  <button class="button button2" @click="addCart"><span>加入购物车</span></button>
                  <button class="button button2" @click="toConfirmOrder"><span>立即购买</span></button>
                </div>
              </div>
            </el-col>
          </el-row>
        </div>
        <div>
          <el-anchor :offset="120" direction="horizontal">
            <el-anchor-link href="#user-evaluation" title="用户评价"/>
            <el-anchor-link href="#parameter-info" title="参数信息"/>
            <el-anchor-link href="#other" title="其他"/>
          </el-anchor>
        </div>

<!--         用户评价-->
        <div  id="user-evaluation">
          <el-empty description="description" />
        </div>
<!--        参数信息-->
        <div  id="parameter-info">
          <el-table :data="skuItemInfo?.groupAttributeValues" >
            <el-table-column prop="groupName" width="180"/>
            <el-table-column>
              <template #default="scope">
                <el-descriptions
                    :column="1"
                >
                  <el-descriptions-item  v-for="item in scope.row.attributes">
                    <template #label>
                      <el-text tag="b" size="large">{{item.attrName}}</el-text>
                    </template>
                    <span v-if="item.attributeValues.length>0" v-for="value in item.attributeValues">{{value}}</span>
                    <span v-else>无</span>
                  </el-descriptions-item>
                </el-descriptions>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </template>
      <!--      骨架屏内容-->
      <template #template>
        <div style="height: 100%">
          <el-row :gutter="20">
            <el-col :span="7" :offset="1">
              <el-skeleton-item variant="image" style="height: 500px"/>
            </el-col>
            <el-col :span="13" :offset="1">
              <div>
                <el-skeleton-item variant="text" style="width: 100%;height: 40px"/>
                <el-skeleton-item variant="text" style="width: 100%;height: 40px;margin-top: 20px"/>
                <div style="margin-top: 30px">
                  <span class="price_class"><el-skeleton-item variant="text" style="height: 30px;width: 80%"/></span>
                </div>
                <div style="margin-top: 150px">
                  <div v-for="item in 2" :key="item">
                    <el-row :gutter="20">
                      <el-col :span="3">
                        <el-skeleton-item variant="text" style="height: 30px"/>
                      </el-col>
                      <el-col :span="3" v-for="(i) in 3" :key="i">
                        <el-skeleton-item variant="text" style="height: 30px"/>
                      </el-col>
                    </el-row>
                  </div>
                </div>
              </div>
            </el-col>
          </el-row>
        </div>
      </template>
    </el-skeleton>
</template>

<script setup lang="ts">
import {ref, onMounted,} from "vue";
import {useRoute, useRouter} from "vue-router";
import SkuPicList from "@/components/re-market/SkuPicList.vue";
import type {SkuItemDetailVO} from'@/api/serve/re-market/sku/type'
import {reqGetSkuDetail} from '@/api/serve/re-market/sku'

let loading = ref(true)
const route = useRoute()
const router = useRouter()
let skuItemInfo = ref<SkuItemDetailVO>()
let showComponentData = ref(-1)//由于子组件需要父组件传递数据，需要确保父组件数据准备好了再加载子组件
let num = ref(1)
let showImg = ref(false)

onMounted(() => {
  if (route.query.skuId) {
    getSkuItem(route.query.skuId)
  }
  setTimeout(() => {
    loading.value = false
  }, 1000)
})

function getSkuItem(skuId) {
  reqGetSkuDetail(skuId).then(res => {
    skuItemInfo.value = res.data
    showImg.value = true
  }).then(() => {
    showComponentData.value = 0//父组件数据已经准备妥当，加载子组件
  })
}

function toConfirmOrder() {
  if (skuItemInfo.value.sku.stock - num.value < 0) {
    return ElMessage({message: "库存不足", type: 'error'})
  }
}

function selectItem(saleAttribute, skuIds) {
  /*因为需要统计所有销售属性选中的sku，所以需要先保存当前选中元素的skuIds数组，然后计算*/
  saleAttribute.selectSkuList = skuIds
  /*遍历所有的分组，获取选中的skuid，然后计算出交集*/
  let arr = skuItemInfo.value?.skuSaleAttrWithValueWithSkuIdsVOList.map(item => {
    return item.selectSkuList
  })
  console.log("统计",JSON.stringify(arr))
  //得出当前的skuId
  let findSkuId = findIntersection(arr)
  getSkuItem(findSkuId)
  //修改路由上的query值
  router.push({query:{
      skuId:findSkuId
    }})
}

/**
 * 获取传入的数组中的交集的值
 * @param arrays
 * @returns {null}
 */
function findIntersection(arrays) {
  // 创建一个 Set 对象来存储所有数组中的唯一元素
  let set = new Set();
  // 遍历每个数组，将其中的元素添加到 Set 对象中
  for (let array of arrays) {
    for (let element of array) {
      set.add(element);
    }
  }
  // 找到交集元素，即唯一存在于所有数组中的元素
  let intersection = null;
  for (let element of set) {
    if (arrays.every(array => array.includes(element))) {
      intersection = element;
      break;
    }
  }
  return intersection;
}


function showComponents(v) {
  showComponentData.value = v
}
</script>

<style scoped>
.price_class {
  font-size: 30px;
  color: red;
}
.skuValue_act_class {
  border: 1px solid #288FC7;
  color: #288FC7;
  padding: 2px 5px;
  /*color: red;*/
}
.button {
  width: 150px;
  height: 50px;
  background-color: #288FC7;
  border: none;
  color: white;
  padding: 15px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: 4px 2px;
  cursor: pointer;
  -webkit-transition-duration: 0.4s;
  transition-duration: 0.4s;
}

.button2:hover {
  box-shadow: 0 12px 16px 0 rgba(0, 0, 0, 0.24), 0 17px 50px 0 rgba(0, 0, 0, 0.19);
  background-color: #1DAEEE;
  color: red;
}
</style>
